<?php

namespace Frieza\Modules\Position\Controllers;

use Doraemon\model\Position\PositionExtend;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Exception;
use Illuminate\Http\Request;

class CustomBannerController extends BaseController
{
    use BaseTrait;

    protected bool $is_search_suggest_rank;
    protected function afterSetPositionId()
    {
        $this->is_search_suggest_rank = $this->positionId == 55;
    }


    /**
     * @throws Exception
     */
    public function saveModule(Request $request, $baseInfoId)
    {
        $base = $request->input('base', []);
        $rules = $request->input('rules', []);
        $pic = $base['pic_id'] ?? '';
        $pos = $request->input('base.pos', 0);
        if ($this->is_search_suggest_rank) {
            $key_word = $request->input('key_word', '');
            $sign = $request->input('sign', '');
            $this->posCheck($pos);
            PositionExtend::saveSearchSuggestRank($baseInfoId, $sign, $key_word);
        }
        else
        {
            $this->posCheck($pos, '轮播图位置');
            if (empty($pic)) throw new Exception('图片必须上传');
            $img_size = $rules['img_size'] ?? 0;
            //修改或者复制,并且图片没有被编辑过,不进行校验
            if ($img_size != 0) $this->formRulesValid($rules, 0);
        }

    }
    public function defaultBaseInfo(Request $request)
    {
        $response = parent::defaultBaseInfo($request);

        $response['pos'] = $this->base_field_pos ?: 1;

        return $response;
    }

    public function beforeTitleForm(Form $form)
    {
        if ($this->is_search_suggest_rank) {
            $posForm = $form
                ->radioButton('base.pos', '展示位置')
                ->options($this->generatePosPosition())->required();
            if ($this->_posHelp) $posForm->help($this->_posHelp);
            $form->text('key_word', '关键词')->required()->help("多个关键词使用,隔开;例如:厨房,客厅");
            $form->radio('sign','推荐')
                ->options([0 => '否', 1 => '是']);
            $form->multiLinkSelect('base.link', '热词链接', $this->bannerAllowLink)->help('仅支持H5落地页');
        }

    }
    public function beforeLinkForm(Form $form)
    {
        if ($this->is_search_suggest_rank)
        {
            //nothing
        }
        else
        {
            $pos = $this->getPositionConfOptions('pos', 5);
            if ($pos > 1) {
                $posForm = $form
                    ->radioButton('base.pos', '轮播图位置')
                    ->options($this->generatePosPosition())->required();
                if ($this->_posHelp) $posForm->help($this->_posHelp);
            }
            $this->bannerForm($form);
        }
    }

    public function extendList(Grid $grid)
    {
        $pos = $this->getPositionConfOptions('pos', 5);
        if ($this->is_search_suggest_rank)
        {
            $grid->column('pos', '展示位置')->label($this->labelColor($pos));
            $grid->column('ext->sign', '推荐标识')->bool();
            $grid->column('ext->key_word', '搜索热词');
        }
        else
        {
            if ($pos > 1) {
                $grid->column('pos', '位置')->label($this->labelColor($pos));
            }

            $grid->column('pic_url', '图片')->image("", 120);
        }
    }

}
